package proj.pt.web;

import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;

import proj.pt.model.Course;
import proj.pt.model.Exam;
import proj.pt.model.SubCourse;
import proj.pt.model.Subject;
import proj.pt.model.User;

public class PerformanceResultController extends CustomSimpleFormController {

	protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
		Map dataMap = super.referenceData(request, command, errors);
		User currentUser = (User)dataMap.get("currentUser");
		PerformanceResultFilter filter = (PerformanceResultFilter)command;
		
		String action = WebUtils.getAction(request);
		
		if("action_course_select".equals(action)){
			// course selected, so load batches and semesters according to that.
			// dataMap.put("batches", getLogicProcessor().getCourseBatches(filter.getCourse()));
			dataMap.put("semesters", getLogicProcessor().getSubCourses(filter.getCourse()));
			
		} else if(filter.getSubCourse() != null){
			// semester already available as selected, so reain the batches and semester list
			// as per this selection.
			if(filter.getSubCourse().getCourse() != null)
				filter.setCourse(getLogicProcessor().getCourse(filter.getSubCourse().getCourse().getId()));
			
			// dataMap.put("batches", getLogicProcessor().getCourseBatches(filter.getSubCourse().getCourse()));
			dataMap.put("semesters", getLogicProcessor().getSubCourses(filter.getSubCourse().getCourse()));
			dataMap.put("subjects", getLogicProcessor().getSubjectBySemester(filter.getSubCourse()));
			dataMap.put("students", getLogicProcessor().getStudents(filter.getSubCourse(), currentUser));
			
		} else if(filter.getCourse() != null){
			// course selected, so load batches and semesters according to that.
			// dataMap.put("batches", getLogicProcessor().getCourseBatches(filter.getCourse()));
			dataMap.put("semesters", getLogicProcessor().getSubCourses(filter.getCourse()));
			
		}//end else if
		
		
		
		dataMap.put("courses", getLogicProcessor().getCourses(currentUser));
		dataMap.put("examTypes", getLogicProcessor().getExamTypes());
		
		
		
		if("action_show".equals(action)){
			dataMap.put("results", getLogicProcessor().getPerformanceMatrix(filter));
			
		} else {
			// filter.setExamResults(null);
			// dataMap.put("examResultsount", 0);
		}
		
		
		Map reportTypes = new TreeMap();
		reportTypes.put("STUDENTS_EXAM", "Students vs Exam"); // select an exam
		reportTypes.put("STUDENT_EXAMS", "Student vs Exams"); // select a Student (AND) Subject
		
		dataMap.put("reportTypes", reportTypes);
		
		return dataMap;
	} //end referenceData
	
	
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(SubCourse.class, "subCourse", new SubCourseEditor(getLogicProcessor()));
		binder.registerCustomEditor(Course.class, "course", new CourseEditor(getLogicProcessor()));
		binder.registerCustomEditor(Subject.class, "subject", new SubjectEditor(getLogicProcessor()));
		binder.registerCustomEditor(User.class, "student", new UserEditor(getLogicProcessor()));
		binder.registerCustomEditor(Exam.class, "exam", new ExamEditor(getLogicProcessor()));
	}
	
	
	/**
	 * This method does the validation.
	 */
	protected void onBindAndValidate(HttpServletRequest request,
            Object command,
            BindException errors) throws Exception {
		
		// if no validation required, then just return from this method.
		if(!doValidate(request))return;
		
				
		PerformanceResultFilter filter = (PerformanceResultFilter)command;
		
		/*if(filter.getSubCourse() == null){
			errors.reject("semester.required", "Semester required.");
		} //end if
		
		if(filter.getExam() == null){
			errors.reject("exam.required", "Exam required.");
		} //end if
*/	}
}
